package com.wondersgroup.robot.web.action;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Transaction;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.wondersgroup.robot.common.Nehnre;
import com.wondersgroup.robot.dao.UserFavoritesDAO;
import com.wondersgroup.robot.dao.UserFavoritesListDAO;
import com.wondersgroup.robot.po.UserFavorites;
import com.wondersgroup.robot.po.UserFavoritesList;

public class UserFavoritesAction {

	public String load(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		String logName = Nehnre.decode(request.getParameter("logName"));
		request.getSession().setAttribute("logName", logName);
		UserFavoritesDAO ufd = new UserFavoritesDAO();
		UserFavoritesListDAO ufld = new UserFavoritesListDAO();
		List ufList = ufd.findByLogName(logName);
		JSONArray ufArr = transferToJsonArray(ufld, ufList);
		JSONArray uflArr = transferToJsonArray(ufld);
		ufd.getSession().clear();
		ufld.getSession().clear();
		return "{ufArr:" + ufArr.toString() + ",uflArr:" + uflArr.toString() + "}";
	}

	/**
	 * @param ufld
	 */
	private JSONArray transferToJsonArray(UserFavoritesListDAO ufld) {
		List uflList = ufld.findAll();
		Object[] uflArray = uflList.toArray();
		Arrays.sort(uflArray, new Comparator(){
			public int compare(Object arg0, Object arg1) {
				return ((UserFavoritesList)arg0).getUflOrderNum().intValue() - ((UserFavoritesList)arg1).getUflOrderNum().intValue();
			}
		});
		JSONArray uflArr = new JSONArray();
		for(int i=0;i<uflArray.length;i++){
			UserFavoritesList ufl = (UserFavoritesList)uflArray[i];
			JSONObject json = new JSONObject();
			json.accumulate("uflName", ufl.getUflName());
			json.accumulate("uflLinkUrl", ufl.getUflLinkUrl());
			json.accumulate("uflPicUrl", ufl.getUflPicUrl());
			json.accumulate("uflId", ufl.getUflId());
			uflArr.add(json.toString());
		}
		return uflArr;
	}

	/**
	 * @param ufld
	 * @param ufList
	 * @return
	 */
	private JSONArray transferToJsonArray(UserFavoritesListDAO ufld, List ufList) {
		Object ufArray[] = ufList.toArray();
		Arrays.sort(ufArray, new Comparator(){
			public int compare(Object arg0, Object arg1) {
				return ((UserFavorites)arg0).getUsfaNum().intValue() - ((UserFavorites)arg1).getUsfaNum().intValue();
			}
		});
		JSONArray jsonArr = new JSONArray();
		for(int i=0;i<ufArray.length;i++){
			UserFavorites uf = (UserFavorites)ufArray[i];
			UserFavoritesList ufl = ufld.findById(uf.getUflId());
			JSONObject json = new JSONObject();
			json.accumulate("usfaNum", uf.getUsfaNum());
			json.accumulate("usfaId", uf.getUsfaId());
			json.accumulate("uflName", ufl.getUflName());
			json.accumulate("uflLinkUrl", ufl.getUflLinkUrl());
			json.accumulate("uflPicUrl", ufl.getUflPicUrl());
			jsonArr.add(json.toString());
		}
		return jsonArr;
	}
	
	public String save(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		String uflId = Nehnre.decode(request.getParameter("uflId"));
		String usfaNum = Nehnre.decode(request.getParameter("usfaNum"));
		String logName = (String)request.getSession().getAttribute("logName");
		if(!"".equals(logName) && !"".equals(uflId) && !"".equals(usfaNum)){
			UserFavoritesDAO ufd = new UserFavoritesDAO();
			UserFavorites uf = new UserFavorites();
			Transaction tx = ufd.getSession().beginTransaction();
			uf.setLogName(logName);
			uf.setUflId(uflId);
			uf.setUsfaNum(Integer.valueOf(usfaNum));
			ufd.save(uf);
			tx.commit();
			return "{success:true,usfaId:\""+uf.getUsfaId()+"\"}";
		}
		return "{success:false}";
	}
	
	public String delete(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		String usfaId = Nehnre.decode(request.getParameter("usfaId"));
		if(!"".equals(usfaId)){
			UserFavoritesDAO ufd = new UserFavoritesDAO();
			Transaction tx = ufd.getSession().beginTransaction();
			UserFavorites uf = ufd.findById(usfaId);
			ufd.delete(uf);
			tx.commit();
			return "{success:true}";
		}
		return "{success:false}";
	}
}
